<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;

use App\Models\Linkslistsminapps;
use App\Models\Linkslists;
use App\Models\Settingminiappsnotice;

use App\Services\EmailService;
use App\Services\MiniappService;
use App\Services\CacheService;
use Log,Exception,DB;

class OfficialPush implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $appid;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($template,$title,$keyword1,$keyword2,$keyword3,$keyword4,$keyword5,$remark,$jumpurl)
    {
        $this->template = $template;
        $this->title = $title;
        $this->keyword1 = $keyword1;
        $this->keyword2 = $keyword2;
        $this->keyword3 = $keyword3;
        $this->keyword4 = $keyword4;
        $this->keyword5 = $keyword5;
        $this->remark = $remark;
        $this->jumpurl = $jumpurl;
    }

    /**
     * 公众号内容通知
     *
     * @return void
     */
    public function handle()
    {
        DB::table("member_orders")
            ->leftjoin('users','users.id','=','member_orders.users_id')
            ->select('member_ordersid','member_orders.users_id','users.wechat_openid')
            ->whereIn('pay_status',[3,4])
            ->where('end_time','<',time())
            ->groupBy('users_id')
            ->orderBy('id','DESC')
            ->chunk(100, function($users) {
                $paramas = [
                        'first' => $this->title,
                        'keyword1' => $this->keyword1,
                        'keyword2' => $this->keyword2,
                        'keyword3' => $this->keyword3,
                        'keyword4' => $this->keyword4,
                        'keyword5' => $this->keyword5,
                        'remark' => $this->remark,
                    ];
                foreach ($users as $user) {
                    WechattmplmsgService::sendSingleTempleteMsg($this->template,$user->wechat_openid,$this->jumpurl,"","",$paramas);
                }
            });
    }
}
